home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / VideoStreamV1.0 / Source / mpegDecodeSrc / decoders.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  39.0 KB  |  1,140 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /*
  22.  * decoders.c
  23.  *
  24.  * This file contains all the routines for Huffman decoding required in 
  25.  * MPEG
  26.  *
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <assert.h>
  31. #include "decoders.h"
  32. #include "util.h" 
  33. #include "video.h"
  34.  
  35. /* Decoding table for coded_block_pattern */
  36.  
  37. coded_block_pattern_entry coded_block_pattern[512] = 
  38. { {(unsigned int)ERROR, 0}, {(unsigned int)ERROR, 0}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
  39.     {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
  40.     {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
  41.     {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
  42.     {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
  43.     {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
  44.     {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
  45.     {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
  46.     {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
  47.     {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
  48.     {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
  49.     {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
  50.     {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
  51.     {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
  52.     {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
  53.     {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
  54.     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  55.     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  56.     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  57.     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  58.     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  59.     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  60.     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  61.     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  62.     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  63.     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  64.     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  65.     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  66.     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  67.     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  68.     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  69.     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  70.     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  71.     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  72.     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  73.     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  74.     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  75.     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  76.     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  77.     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  78.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  79.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  80.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  81.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  82.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  83.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  84.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  85.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  86.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  87.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  88.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  89.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
  90.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  91.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  92.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  93.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  94.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  95.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  96.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  97.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  98.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  99.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  100.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  101.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}
  102. };
  103.  
  104. /* Decoding table for dct_dc_size_luminance */
  105. dct_dc_size_entry dct_dc_size_luminance[128] =
  106. {   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  107.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  108.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  109.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  110.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  111.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  112.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  113.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  114.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  115.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  116.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  117.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  118.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  119.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  120.     {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, 
  121.     {6, 5}, {6, 5}, {6, 5}, {6, 5}, {7, 6}, {7, 6}, {8, 7}, {(unsigned int)ERROR, 0}
  122. };
  123.  
  124. /* Decoding table for dct_dc_size_chrominance */
  125. dct_dc_size_entry dct_dc_size_chrominance[256] =
  126. { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  127.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  128.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  129.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  130.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  131.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  132.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  133.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  134.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  135.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  136.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  137.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  138.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  139.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  140.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  141.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  142.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  143.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  144.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  145.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  146.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  147.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  148.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  149.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  150.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  151.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  152.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  153.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  154.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  155.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  156.     {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, 
  157.     {6, 6}, {6, 6}, {6, 6}, {6, 6}, {7, 7}, {7, 7}, {8, 8}, {(unsigned int)ERROR, 0}
  158. };
  159.  
  160. /* DCT coeff tables. */
  161.  
  162. unsigned short int dct_coeff_tbl_0[256] =
  163. {
  164. 0xffff, 0xffff, 0xffff, 0xffff, 
  165. 0xffff, 0xffff, 0xffff, 0xffff, 
  166. 0xffff, 0xffff, 0xffff, 0xffff, 
  167. 0xffff, 0xffff, 0xffff, 0xffff, 
  168. 0x052f, 0x051f, 0x050f, 0x04ff, 
  169. 0x183f, 0x402f, 0x3c2f, 0x382f, 
  170. 0x342f, 0x302f, 0x2c2f, 0x7c1f, 
  171. 0x781f, 0x741f, 0x701f, 0x6c1f, 
  172. 0x028e, 0x028e, 0x027e, 0x027e, 
  173. 0x026e, 0x026e, 0x025e, 0x025e, 
  174. 0x024e, 0x024e, 0x023e, 0x023e, 
  175. 0x022e, 0x022e, 0x021e, 0x021e, 
  176. 0x020e, 0x020e, 0x04ee, 0x04ee, 
  177. 0x04de, 0x04de, 0x04ce, 0x04ce, 
  178. 0x04be, 0x04be, 0x04ae, 0x04ae, 
  179. 0x049e, 0x049e, 0x048e, 0x048e, 
  180. 0x01fd, 0x01fd, 0x01fd, 0x01fd, 
  181. 0x01ed, 0x01ed, 0x01ed, 0x01ed, 
  182. 0x01dd, 0x01dd, 0x01dd, 0x01dd, 
  183. 0x01cd, 0x01cd, 0x01cd, 0x01cd, 
  184. 0x01bd, 0x01bd, 0x01bd, 0x01bd, 
  185. 0x01ad, 0x01ad, 0x01ad, 0x01ad, 
  186. 0x019d, 0x019d, 0x019d, 0x019d, 
  187. 0x018d, 0x018d, 0x018d, 0x018d, 
  188. 0x017d, 0x017d, 0x017d, 0x017d, 
  189. 0x016d, 0x016d, 0x016d, 0x016d, 
  190. 0x015d, 0x015d, 0x015d, 0x015d, 
  191. 0x014d, 0x014d, 0x014d, 0x014d, 
  192. 0x013d, 0x013d, 0x013d, 0x013d, 
  193. 0x012d, 0x012d, 0x012d, 0x012d, 
  194. 0x011d, 0x011d, 0x011d, 0x011d, 
  195. 0x010d, 0x010d, 0x010d, 0x010d, 
  196. 0x282c, 0x282c, 0x282c, 0x282c, 
  197. 0x282c, 0x282c, 0x282c, 0x282c, 
  198. 0x242c, 0x242c, 0x242c, 0x242c, 
  199. 0x242c, 0x242c, 0x242c, 0x242c, 
  200. 0x143c, 0x143c, 0x143c, 0x143c, 
  201. 0x143c, 0x143c, 0x143c, 0x143c, 
  202. 0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  203. 0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  204. 0x085c, 0x085c, 0x085c, 0x085c, 
  205. 0x085c, 0x085c, 0x085c, 0x085c, 
  206. 0x047c, 0x047c, 0x047c, 0x047c, 
  207. 0x047c, 0x047c, 0x047c, 0x047c, 
  208. 0x046c, 0x046c, 0x046c, 0x046c, 
  209. 0x046c, 0x046c, 0x046c, 0x046c, 
  210. 0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  211. 0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  212. 0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  213. 0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  214. 0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  215. 0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  216. 0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  217. 0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  218. 0x681c, 0x681c, 0x681c, 0x681c, 
  219. 0x681c, 0x681c, 0x681c, 0x681c, 
  220. 0x641c, 0x641c, 0x641c, 0x641c, 
  221. 0x641c, 0x641c, 0x641c, 0x641c, 
  222. 0x601c, 0x601c, 0x601c, 0x601c, 
  223. 0x601c, 0x601c, 0x601c, 0x601c, 
  224. 0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  225. 0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  226. 0x581c, 0x581c, 0x581c, 0x581c, 
  227. 0x581c, 0x581c, 0x581c, 0x581c, 
  228. };
  229.  
  230. unsigned short int dct_coeff_tbl_1[16] = 
  231. {
  232. 0x00bb, 0x202b, 0x103b, 0x00ab, 
  233. 0x084b, 0x1c2b, 0x541b, 0x501b, 
  234. 0x009b, 0x4c1b, 0x481b, 0x045b, 
  235. 0x0c3b, 0x008b, 0x182b, 0x441b, 
  236. };
  237.  
  238. unsigned short int dct_coeff_tbl_2[4] =
  239. {
  240. 0x4019, 0x1429, 0x0079, 0x0839, 
  241. };
  242.  
  243. unsigned short int dct_coeff_tbl_3[4] = 
  244. {
  245. 0x0449, 0x3c19, 0x3819, 0x1029, 
  246. };
  247.  
  248. unsigned short int dct_coeff_next[256] = 
  249. {
  250. 0xffff, 0xffff, 0xffff, 0xffff, 
  251. 0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  252. 0x0826, 0x0826, 0x2416, 0x2416, 
  253. 0x0046, 0x0046, 0x2016, 0x2016, 
  254. 0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  255. 0x1815, 0x1815, 0x1815, 0x1815, 
  256. 0x0425, 0x0425, 0x0425, 0x0425, 
  257. 0x1415, 0x1415, 0x1415, 0x1415, 
  258. 0x3417, 0x0067, 0x3017, 0x2c17, 
  259. 0x0c27, 0x0437, 0x0057, 0x2817, 
  260. 0x0034, 0x0034, 0x0034, 0x0034, 
  261. 0x0034, 0x0034, 0x0034, 0x0034, 
  262. 0x1014, 0x1014, 0x1014, 0x1014, 
  263. 0x1014, 0x1014, 0x1014, 0x1014, 
  264. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  265. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  266. 0x0023, 0x0023, 0x0023, 0x0023, 
  267. 0x0023, 0x0023, 0x0023, 0x0023, 
  268. 0x0023, 0x0023, 0x0023, 0x0023, 
  269. 0x0023, 0x0023, 0x0023, 0x0023, 
  270. 0x0813, 0x0813, 0x0813, 0x0813, 
  271. 0x0813, 0x0813, 0x0813, 0x0813, 
  272. 0x0813, 0x0813, 0x0813, 0x0813, 
  273. 0x0813, 0x0813, 0x0813, 0x0813, 
  274. 0x0412, 0x0412, 0x0412, 0x0412, 
  275. 0x0412, 0x0412, 0x0412, 0x0412, 
  276. 0x0412, 0x0412, 0x0412, 0x0412, 
  277. 0x0412, 0x0412, 0x0412, 0x0412, 
  278. 0x0412, 0x0412, 0x0412, 0x0412, 
  279. 0x0412, 0x0412, 0x0412, 0x0412, 
  280. 0x0412, 0x0412, 0x0412, 0x0412, 
  281. 0x0412, 0x0412, 0x0412, 0x0412, 
  282. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  283. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  284. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  285. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  286. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  287. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  288. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  289. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  290. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  291. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  292. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  293. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  294. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  295. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  296. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  297. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  298. 0x0011, 0x0011, 0x0011, 0x0011, 
  299. 0x0011, 0x0011, 0x0011, 0x0011, 
  300. 0x0011, 0x0011, 0x0011, 0x0011, 
  301. 0x0011, 0x0011, 0x0011, 0x0011, 
  302. 0x0011, 0x0011, 0x0011, 0x0011, 
  303. 0x0011, 0x0011, 0x0011, 0x0011, 
  304. 0x0011, 0x0011, 0x0011, 0x0011, 
  305. 0x0011, 0x0011, 0x0011, 0x0011, 
  306. 0x0011, 0x0011, 0x0011, 0x0011, 
  307. 0x0011, 0x0011, 0x0011, 0x0011, 
  308. 0x0011, 0x0011, 0x0011, 0x0011, 
  309. 0x0011, 0x0011, 0x0011, 0x0011, 
  310. 0x0011, 0x0011, 0x0011, 0x0011, 
  311. 0x0011, 0x0011, 0x0011, 0x0011, 
  312. 0x0011, 0x0011, 0x0011, 0x0011, 
  313. 0x0011, 0x0011, 0x0011, 0x0011, 
  314. };
  315.  
  316. unsigned short int dct_coeff_first[256] = 
  317. {
  318. 0xffff, 0xffff, 0xffff, 0xffff, 
  319. 0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  320. 0x0826, 0x0826, 0x2416, 0x2416, 
  321. 0x0046, 0x0046, 0x2016, 0x2016, 
  322. 0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  323. 0x1815, 0x1815, 0x1815, 0x1815, 
  324. 0x0425, 0x0425, 0x0425, 0x0425, 
  325. 0x1415, 0x1415, 0x1415, 0x1415, 
  326. 0x3417, 0x0067, 0x3017, 0x2c17, 
  327. 0x0c27, 0x0437, 0x0057, 0x2817, 
  328. 0x0034, 0x0034, 0x0034, 0x0034, 
  329. 0x0034, 0x0034, 0x0034, 0x0034, 
  330. 0x1014, 0x1014, 0x1014, 0x1014, 
  331. 0x1014, 0x1014, 0x1014, 0x1014, 
  332. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  333. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  334. 0x0023, 0x0023, 0x0023, 0x0023, 
  335. 0x0023, 0x0023, 0x0023, 0x0023, 
  336. 0x0023, 0x0023, 0x0023, 0x0023, 
  337. 0x0023, 0x0023, 0x0023, 0x0023, 
  338. 0x0813, 0x0813, 0x0813, 0x0813, 
  339. 0x0813, 0x0813, 0x0813, 0x0813, 
  340. 0x0813, 0x0813, 0x0813, 0x0813, 
  341. 0x0813, 0x0813, 0x0813, 0x0813, 
  342. 0x0412, 0x0412, 0x0412, 0x0412, 
  343. 0x0412, 0x0412, 0x0412, 0x0412, 
  344. 0x0412, 0x0412, 0x0412, 0x0412, 
  345. 0x0412, 0x0412, 0x0412, 0x0412, 
  346. 0x0412, 0x0412, 0x0412, 0x0412, 
  347. 0x0412, 0x0412, 0x0412, 0x0412, 
  348. 0x0412, 0x0412, 0x0412, 0x0412, 
  349. 0x0412, 0x0412, 0x0412, 0x0412, 
  350. 0x0010, 0x0010, 0x0010, 0x0010, 
  351. 0x0010, 0x0010, 0x0010, 0x0010, 
  352. 0x0010, 0x0010, 0x0010, 0x0010, 
  353. 0x0010, 0x0010, 0x0010, 0x0010, 
  354. 0x0010, 0x0010, 0x0010, 0x0010, 
  355. 0x0010, 0x0010, 0x0010, 0x0010, 
  356. 0x0010, 0x0010, 0x0010, 0x0010, 
  357. 0x0010, 0x0010, 0x0010, 0x0010, 
  358. 0x0010, 0x0010, 0x0010, 0x0010, 
  359. 0x0010, 0x0010, 0x0010, 0x0010, 
  360. 0x0010, 0x0010, 0x0010, 0x0010, 
  361. 0x0010, 0x0010, 0x0010, 0x0010, 
  362. 0x0010, 0x0010, 0x0010, 0x0010, 
  363. 0x0010, 0x0010, 0x0010, 0x0010, 
  364. 0x0010, 0x0010, 0x0010, 0x0010, 
  365. 0x0010, 0x0010, 0x0010, 0x0010, 
  366. 0x0010, 0x0010, 0x0010, 0x0010, 
  367. 0x0010, 0x0010, 0x0010, 0x0010, 
  368. 0x0010, 0x0010, 0x0010, 0x0010, 
  369. 0x0010, 0x0010, 0x0010, 0x0010, 
  370. 0x0010, 0x0010, 0x0010, 0x0010, 
  371. 0x0010, 0x0010, 0x0010, 0x0010, 
  372. 0x0010, 0x0010, 0x0010, 0x0010, 
  373. 0x0010, 0x0010, 0x0010, 0x0010, 
  374. 0x0010, 0x0010, 0x0010, 0x0010, 
  375. 0x0010, 0x0010, 0x0010, 0x0010, 
  376. 0x0010, 0x0010, 0x0010, 0x0010, 
  377. 0x0010, 0x0010, 0x0010, 0x0010, 
  378. 0x0010, 0x0010, 0x0010, 0x0010, 
  379. 0x0010, 0x0010, 0x0010, 0x0010, 
  380. 0x0010, 0x0010, 0x0010, 0x0010, 
  381. 0x0010, 0x0010, 0x0010, 0x0010, 
  382. };
  383.  
  384. /* Macro for filling up the decoding table for mb_addr_inc */
  385. #define ASSIGN1(start, end, step, val, num) \
  386.   for (i = start; i < end; i+= step) { \
  387.     for (j = 0; j < step; j++) { \
  388.       mb_addr_inc[i+j].value = val; \
  389.       mb_addr_inc[i+j].num_bits = num; \
  390.     } \
  391.     val--; \
  392.     }
  393.  
  394.  
  395.  
  396. /*
  397.  *--------------------------------------------------------------
  398.  *
  399.  * init_mb_addr_inc --
  400.  *
  401.  *    Initialize the VLC decoding table for macro_block_address_increment
  402.  *
  403.  * Results:
  404.  *    The decoding table for macro_block_address_increment will
  405.  *      be filled; illegal values will be filled as ERROR.
  406.  *
  407.  * Side effects:
  408.  *    The global array mb_addr_inc will be filled.
  409.  *
  410.  *--------------------------------------------------------------
  411.  */
  412. static void
  413. init_mb_addr_inc()
  414. {
  415.   int i, j, val;
  416.  
  417.   for (i = 0; i < 8; i++) {
  418.     mb_addr_inc[i].value = ERROR;
  419.     mb_addr_inc[i].num_bits = 0;
  420.   }
  421.  
  422.   mb_addr_inc[8].value = MACRO_BLOCK_ESCAPE;
  423.   mb_addr_inc[8].num_bits = 11;
  424.  
  425.   for (i = 9; i < 15; i++) {
  426.     mb_addr_inc[i].value = ERROR;
  427.     mb_addr_inc[i].num_bits = 0;
  428.   }
  429.  
  430.   mb_addr_inc[15].value = MACRO_BLOCK_STUFFING;
  431.   mb_addr_inc[15].num_bits = 11;
  432.  
  433.   for (i = 16; i < 24; i++) {
  434.     mb_addr_inc[i].value = ERROR;
  435.     mb_addr_inc[i].num_bits = 0;
  436.   }
  437.  
  438.   val = 33;
  439.  
  440.   ASSIGN1(24, 36, 1, val, 11);
  441.   ASSIGN1(36, 48, 2, val, 10);
  442.   ASSIGN1(48, 96, 8, val, 8);
  443.   ASSIGN1(96, 128, 16, val, 7);
  444.   ASSIGN1(128, 256, 64, val, 5);
  445.   ASSIGN1(256, 512, 128, val, 4);
  446.   ASSIGN1(512, 1024, 256, val, 3);
  447.   ASSIGN1(1024, 2048, 1024, val, 1);
  448. }
  449.  
  450.  
  451. /* Macro for filling up the decoding table for mb_type */
  452. #define ASSIGN2(start, end, quant, motion_forward, motion_backward, pattern, intra, num, mb_type) \
  453.   for (i = start; i < end; i ++) { \
  454.     mb_type[i].mb_quant = quant; \
  455.     mb_type[i].mb_motion_forward = motion_forward; \
  456.     mb_type[i].mb_motion_backward = motion_backward; \
  457.     mb_type[i].mb_pattern = pattern; \
  458.     mb_type[i].mb_intra = intra; \
  459.     mb_type[i].num_bits = num; \
  460.   }
  461.      
  462.  
  463.  
  464. /*
  465.  *--------------------------------------------------------------
  466.  *
  467.  * init_mb_type_P --
  468.  *
  469.  *    Initialize the VLC decoding table for macro_block_type in
  470.  *      predictive-coded pictures.
  471.  *
  472.  * Results:
  473.  *    The decoding table for macro_block_type in predictive-coded
  474.  *      pictures will be filled; illegal values will be filled as ERROR.
  475.  *
  476.  * Side effects:
  477.  *    The global array mb_type_P will be filled.
  478.  *
  479.  *--------------------------------------------------------------
  480.  */
  481. static void
  482. init_mb_type_P()
  483. {
  484.   int i;
  485.  
  486.   mb_type_P[0].mb_quant = mb_type_P[0].mb_motion_forward 
  487.     = mb_type_P[0].mb_motion_backward = mb_type_P[0].mb_pattern 
  488.       = mb_type_P[0].mb_intra = ERROR;
  489.   mb_type_P[0].num_bits = 0;
  490.  
  491.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_P)
  492.   ASSIGN2(2, 4, 1, 0, 0, 1, 0, 5, mb_type_P)
  493.   ASSIGN2(4, 6, 1, 1, 0, 1, 0, 5, mb_type_P);
  494.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_P);
  495.   ASSIGN2(8, 16, 0, 1, 0, 0, 0, 3, mb_type_P);
  496.   ASSIGN2(16, 32, 0, 0, 0, 1, 0, 2, mb_type_P);
  497.   ASSIGN2(32, 64, 0, 1, 0, 1, 0, 1, mb_type_P);
  498. }
  499.  
  500.  
  501.  
  502.  
  503. /*
  504.  *--------------------------------------------------------------
  505.  *
  506.  * init_mb_type_B --
  507.  *
  508.  *    Initialize the VLC decoding table for macro_block_type in
  509.  *      bidirectionally-coded pictures.
  510.  *
  511.  * Results:
  512.  *    The decoding table for macro_block_type in bidirectionally-coded
  513.  *      pictures will be filled; illegal values will be filled as ERROR.
  514.  *
  515.  * Side effects:
  516.  *    The global array mb_type_B will be filled.
  517.  *
  518.  *--------------------------------------------------------------
  519.  */
  520. static void
  521. init_mb_type_B()
  522. {
  523.   int i;
  524.  
  525.   mb_type_B[0].mb_quant = mb_type_B[0].mb_motion_forward 
  526.     = mb_type_B[0].mb_motion_backward = mb_type_B[0].mb_pattern 
  527.       = mb_type_B[0].mb_intra = ERROR;
  528.   mb_type_B[0].num_bits = 0;
  529.  
  530.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_B);
  531.   ASSIGN2(2, 3, 1, 0, 1, 1, 0, 6, mb_type_B);
  532.   ASSIGN2(3, 4, 1, 1, 0, 1, 0, 6, mb_type_B);
  533.   ASSIGN2(4, 6, 1, 1, 1, 1, 0, 5, mb_type_B);
  534.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_B);
  535.   ASSIGN2(8, 12, 0, 1, 0, 0, 0, 4, mb_type_B);
  536.   ASSIGN2(12, 16, 0, 1, 0, 1, 0, 4, mb_type_B);
  537.   ASSIGN2(16, 24, 0, 0, 1, 0, 0, 3, mb_type_B);
  538.   ASSIGN2(24, 32, 0, 0, 1, 1, 0, 3, mb_type_B);
  539.   ASSIGN2(32, 48, 0, 1, 1, 0, 0, 2, mb_type_B);
  540.   ASSIGN2(48, 64, 0, 1, 1, 1, 0, 2, mb_type_B);
  541. }
  542.  
  543.  
  544. /* Macro for filling up the decoding tables for motion_vectors */
  545. #define ASSIGN3(start, end, step, val, num) \
  546.   for (i = start; i < end; i+= step) { \
  547.     for (j = 0; j < step / 2; j++) { \
  548.       motion_vectors[i+j].code = val; \
  549.       motion_vectors[i+j].num_bits = num; \
  550.     } \
  551.     for (j = step / 2; j < step; j++) { \
  552.       motion_vectors[i+j].code = -val; \
  553.       motion_vectors[i+j].num_bits = num; \
  554.     } \
  555.     val--; \
  556.   }
  557.  
  558.  
  559.  
  560. /*
  561.  *--------------------------------------------------------------
  562.  *
  563.  * init_motion_vectors --
  564.  *
  565.  *    Initialize the VLC decoding table for the various motion
  566.  *      vectors, including motion_horizontal_forward_code, 
  567.  *      motion_vertical_forward_code, motion_horizontal_backward_code,
  568.  *      and motion_vertical_backward_code.
  569.  *
  570.  * Results:
  571.  *    The decoding table for the motion vectors will be filled;
  572.  *      illegal values will be filled as ERROR.
  573.  *
  574.  * Side effects:
  575.  *    The global array motion_vector will be filled.
  576.  *
  577.  *--------------------------------------------------------------
  578.  */
  579. static void
  580. init_motion_vectors()
  581. {
  582.   int i, j, val = 16;
  583.  
  584.   for (i = 0; i < 24; i++) {
  585.     motion_vectors[i].code = ERROR;
  586.     motion_vectors[i].num_bits = 0;
  587.   }
  588.  
  589.   ASSIGN3(24, 36, 2, val, 11);
  590.   ASSIGN3(36, 48, 4, val, 10);
  591.   ASSIGN3(48, 96, 16, val, 8);
  592.   ASSIGN3(96, 128, 32, val, 7);
  593.   ASSIGN3(128, 256, 128, val, 5);
  594.   ASSIGN3(256, 512, 256, val, 4);
  595.   ASSIGN3(512, 1024, 512, val, 3);
  596.   ASSIGN3(1024, 2048, 1024, val, 1);
  597. }
  598.  
  599.  
  600.  
  601.  
  602. /*
  603.  *--------------------------------------------------------------
  604.  *
  605.  * init_tables --
  606.  *
  607.  *    Initialize all the tables for VLC decoding; this must be
  608.  *      called when the system is set up before any decoding can
  609.  *      take place.
  610.  *
  611.  * Results:
  612.  *    All the decoding tables will be filled accordingly.
  613.  *
  614.  * Side effects:
  615.  *    The corresponding global array for each decoding table 
  616.  *      will be filled.
  617.  *
  618.  *--------------------------------------------------------------
  619.  */    
  620. void
  621. init_tables()
  622. {
  623.   extern void init_pre_idct();
  624.  
  625.   init_mb_addr_inc();
  626.   init_mb_type_P();
  627.   init_mb_type_B();
  628.   init_motion_vectors();
  629.   init_pre_idct();
  630.  
  631. }
  632.  
  633.  
  634.  
  635. /*
  636.  *--------------------------------------------------------------
  637.  *
  638.  * DecodeMBAddrInc --
  639.  *
  640.  *    Huffman Decoder for macro_block_address_increment; the location
  641.  *      in which the result will be placed is being passed as argument.
  642.  *      The decoded value is obtained by doing a table lookup on
  643.  *      mb_addr_inc.
  644.  *
  645.  * Results:
  646.  *    The decoded value for macro_block_address_increment or ERROR
  647.  *      for unbound values will be placed in the location specified.
  648.  *
  649.  * Side effects:
  650.  *    Bit stream is irreversibly parsed.
  651.  *
  652.  *--------------------------------------------------------------
  653.  */      
  654. void
  655. DecodeMBAddrInc(value)
  656. unsigned int *value;
  657. {
  658.   unsigned int index;
  659.  
  660.   show_bits11(&index);
  661.  
  662.   *value = mb_addr_inc[index].value;
  663.  
  664.   flush_bits(mb_addr_inc[index].num_bits);
  665. }
  666.  
  667.  
  668.  
  669. /*
  670.  *--------------------------------------------------------------
  671.  *
  672.  * DecodeMBTypeI --
  673.  *
  674.  *    Huffman Decoder for macro_block_type in intra-coded pictures;
  675.  *      locations in which the decoded results: macroblock_quant,
  676.  *      macroblock_motion_forward, macro_block_motion_backward,
  677.  *      macroblock_pattern, macro_block_intra, will be placed are
  678.  *      being passed as argument.
  679.  *
  680.  * Results:
  681.  *    The various decoded values for macro_block_type in intra-coded
  682.  *      pictures or ERROR for unbound values will be placed in the 
  683.  *      locations specified.
  684.  *
  685.  * Side effects:
  686.  *    Bit stream is irreversibly parsed.
  687.  *
  688.  *--------------------------------------------------------------
  689.  */
  690. void      
  691. DecodeMBTypeI( mb_quant, mb_motion_forward, mb_motion_backward, mb_pattern, mb_intra)
  692. unsigned int *mb_quant;
  693. unsigned int *mb_motion_forward;
  694. unsigned int *mb_motion_backward;
  695. unsigned int *mb_pattern;
  696. unsigned int *mb_intra;
  697. {
  698.   unsigned int index;
  699.  
  700.   show_bits2(&index);
  701.  
  702.   *mb_motion_forward = 0;
  703.   *mb_motion_backward = 0;
  704.   *mb_pattern = 0;
  705.   *mb_intra = 1;
  706.  
  707.   if (index == 1) {
  708.     *mb_quant = 1;
  709.     flush_bits(2);
  710.   }
  711.   else if (index >= 2) {
  712.     *mb_quant = 0;
  713.     flush_bits(1);
  714.   }
  715.   else 
  716.     *mb_quant = ERROR;
  717. }
  718.  
  719.  
  720.  
  721. /*
  722.  *--------------------------------------------------------------
  723.  *
  724.  * DecodeMBTypeP --
  725.  *
  726.  *    Huffman Decoder for macro_block_type in predictive-coded pictures;
  727.  *      locations in which the decoded results: macroblock_quant,
  728.  *      macroblock_motion_forward, macro_block_motion_backward,
  729.  *      macroblock_pattern, macro_block_intra, will be placed are
  730.  *      being passed as argument. The decoded values are obtained by
  731.  *      doing a table lookup on mb_type_P.
  732.  *
  733.  * Results:
  734.  *    The various decoded values for macro_block_type in 
  735.  *      predictive-coded pictures or ERROR for unbound values will be 
  736.  *      placed in the locations specified.
  737.  *
  738.  * Side effects:
  739.  *    Bit stream is irreversibly parsed.
  740.  *
  741.  *--------------------------------------------------------------
  742.  */       
  743. void 
  744. DecodeMBTypeP(mb_quant, mb_motion_forward, mb_motion_backward, mb_pattern, mb_intra)
  745. unsigned int *mb_quant;
  746. unsigned int *mb_motion_forward;
  747. unsigned int *mb_motion_backward;
  748. unsigned int *mb_pattern;
  749. unsigned int *mb_intra;
  750. {
  751.   unsigned int index;
  752.  
  753.   show_bits6(&index);
  754.  
  755.   *mb_quant = mb_type_P[index].mb_quant;
  756.   *mb_motion_forward = mb_type_P[index].mb_motion_forward;
  757.   *mb_motion_backward = mb_type_P[index].mb_motion_backward;
  758.   *mb_pattern = mb_type_P[index].mb_pattern;
  759.   *mb_intra = mb_type_P[index].mb_intra;
  760.  
  761.   flush_bits(mb_type_P[index].num_bits);
  762. }
  763.  
  764.  
  765.  
  766. /*
  767.  *--------------------------------------------------------------
  768.  *
  769.  * DecodeMBTypeB --
  770.  *
  771.  *    Huffman Decoder for macro_block_type in bidirectionally-coded 
  772.  *      pictures;locations in which the decoded results: macroblock_quant,
  773.  *      macroblock_motion_forward, macro_block_motion_backward,
  774.  *      macroblock_pattern, macro_block_intra, will be placed are
  775.  *      being passed as argument. The decoded values are obtained by
  776.  *      doing a table lookup on mb_type_B.
  777.  *
  778.  * Results:
  779.  *    The various decoded values for macro_block_type in 
  780.  *      bidirectionally-coded pictures or ERROR for unbound values will 
  781.  *      be placed in the locations specified.
  782.  *
  783.  * Side effects:
  784.  *    Bit stream is irreversibly parsed.
  785.  *
  786.  *--------------------------------------------------------------
  787.  */
  788. void        
  789. DecodeMBTypeB(mb_quant, mb_motion_forward, mb_motion_backward, mb_pattern, mb_intra)
  790. unsigned int *mb_quant;
  791. unsigned int *mb_motion_forward;
  792. unsigned int *mb_motion_backward;
  793. unsigned int *mb_pattern;
  794. unsigned int *mb_intra;
  795. {
  796.   unsigned int index;
  797.  
  798.   show_bits6(&index);
  799.  
  800.   *mb_quant = mb_type_B[index].mb_quant;
  801.   *mb_motion_forward = mb_type_B[index].mb_motion_forward;
  802.   *mb_motion_backward = mb_type_B[index].mb_motion_backward;
  803.   *mb_pattern = mb_type_B[index].mb_pattern;
  804.   *mb_intra = mb_type_B[index].mb_intra;
  805.  
  806.   flush_bits(mb_type_B[index].num_bits);
  807. }
  808.  
  809.  
  810.  
  811.  
  812. /*
  813.  *--------------------------------------------------------------
  814.  *
  815.  * DecodeCBP -- 
  816.  *
  817.  *    Huffman Decoder for coded_block_pattern; location in which the
  818.  *      decoded result will be placed is being passed as argument. The
  819.  *      decoded values are obtained by doing a table lookup on 
  820.  *      coded_block_pattern.
  821.  *
  822.  * Results:
  823.  *    The decoded value for coded_block_pattern or ERROR for unbound
  824.  *      values will be placed in the location specified.
  825.  *
  826.  * Side effects:
  827.  *    Bit stream is irreversibly parsed.
  828.  *
  829.  *--------------------------------------------------------------
  830.  */        
  831. void
  832. DecodeCBP(cbp)
  833. unsigned int *cbp;
  834. {
  835.   unsigned int index;
  836.  
  837.   show_bits9(&index);
  838.  
  839.   *cbp = coded_block_pattern[index].cbp;
  840.  
  841.   flush_bits(coded_block_pattern[index].num_bits);
  842. }
  843.  
  844.  
  845.  
  846. /*
  847.  *--------------------------------------------------------------
  848.  *
  849.  * DecodeMotionVectors -- 
  850.  *
  851.  *    Huffman Decoder for the various motion vectors, including
  852.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  853.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  854.  *      Location where the decoded result will be placed is being passed 
  855.  *      as argument. The decoded values are obtained by doing a table
  856.  *      lookup on motion_vectors.
  857.  *
  858.  * Results:
  859.  *    The decoded value for the motion vector or ERROR for unbound
  860.  *      values will be placed in the location specified.
  861.  *
  862.  * Side effects:
  863.  *    Bit stream is irreversibly parsed.
  864.  *
  865.  *--------------------------------------------------------------
  866.  */   
  867. void     
  868. DecodeMotionVectors(code)
  869. int *code;
  870. {
  871.   unsigned int index;
  872.  
  873.   show_bits11(&index);
  874.  
  875.   *code = motion_vectors[index].code;
  876.  
  877.   flush_bits(motion_vectors[index].num_bits);
  878. }
  879.  
  880.  
  881.  
  882.  
  883. /*
  884.  *--------------------------------------------------------------
  885.  *
  886.  * DecodeDCTDCSizeLum --
  887.  *
  888.  *    Huffman Decoder for dct_dc_size_luminance; location where
  889.  *      the result of decoding will be placed is passed as argument.
  890.  *      The decoded values are obtained by doing a table lookup on
  891.  *      dct_dc_size_luminance.
  892.  *
  893.  * Results:
  894.  *    The decoded value for dct_dc_size_luminance or ERROR for 
  895.  *      unbound values will be placed in the location specified.
  896.  *
  897.  * Side effects:
  898.  *    Bit stream is irreversibly parsed.
  899.  *
  900.  *--------------------------------------------------------------
  901.  */        
  902. void
  903. decodeDCTDCSizeLum(value)
  904. unsigned int *value;
  905. {
  906.   unsigned int index;
  907.  
  908.   show_bits7(&index);
  909.   
  910.   *value = dct_dc_size_luminance[index].value;
  911.  
  912.   flush_bits(dct_dc_size_luminance[index].num_bits);
  913. }
  914.  
  915.  
  916.  
  917.  
  918. /*
  919.  *--------------------------------------------------------------
  920.  *
  921.  * DecodeDCTDCSizeChrom --
  922.  *
  923.  *    Huffman Decoder for dct_dc_size_chrominance; location where
  924.  *      the result of decoding will be placed is passed as argument.
  925.  *      The decoded values are obtained by doing a table lookup on
  926.  *      dct_dc_size_chrominance.
  927.  *
  928.  * Results:
  929.  *    The decoded value for dct_dc_size_chrominance or ERROR for
  930.  *      unbound values will be placed in the location specified.
  931.  *
  932.  * Side effects:
  933.  *    Bit stream is irreversibly parsed.
  934.  *
  935.  *--------------------------------------------------------------
  936.  */    
  937. void    
  938. decodeDCTDCSizeChrom(value)
  939. unsigned int *value;
  940. {
  941.   unsigned int index;
  942.  
  943.   show_bits8(&index);
  944.   
  945.   *value = dct_dc_size_chrominance[index].value;
  946.  
  947.   flush_bits(dct_dc_size_chrominance[index].num_bits);
  948. }
  949.  
  950.  
  951.  
  952. /*
  953.  *--------------------------------------------------------------
  954.  *
  955.  * decodeDCTCoeff --
  956.  *
  957.  *    Huffman Decoder for dct_coeff_first and dct_coeff_next;
  958.  *      locations where the results of decoding: run and level, are to
  959.  *      be placed and also the type of DCT coefficients, either
  960.  *      DCT_COEFF_FIRST or DCT_COEFF_NEXT, are being passed as argument.
  961.  *      
  962.  *      The decoder first examines the next 8 bits in the input stream,
  963.  *      and perform according to the following cases:
  964.  *      
  965.  *      '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
  966.  *                    perform a table lookup on dct_coeff_tbl_0.
  967.  *                    One more bit is then examined to determine the sign
  968.  *                    of level.
  969.  *
  970.  *      '0000 0001' - examine 4 more bits (i.e. 12 bits total) and 
  971.  *                    perform a table lookup on dct_coeff_tbl_1.
  972.  *                    One more bit is then examined to determine the sign
  973.  *                    of level.
  974.  *      
  975.  *      '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
  976.  *                    perform a table lookup on dct_coeff_tbl_2.
  977.  *                    One more bit is then examined to determine the sign
  978.  *                    of level.
  979.  *
  980.  *      '0000 0011' - examine 2 more bits (i.e. 10 bits total) and 
  981.  *                    perform a table lookup on dct_coeff_tbl_3.
  982.  *                    One more bit is then examined to determine the sign
  983.  *                    of level.
  984.  *
  985.  *      otherwise   - perform a table lookup on dct_coeff_tbl. If the
  986.  *                    value of run is not ESCAPE, extract one more bit
  987.  *                    to determine the sign of level; otherwise 6 more
  988.  *                    bits will be extracted to obtain the actual value 
  989.  *                    of run , and then 8 or 16 bits to get the value of level.
  990.  *                    
  991.  *      
  992.  *
  993.  * Results:
  994.  *    The decoded values of run and level or ERROR for unbound values
  995.  *      are placed in the locations specified.
  996.  *
  997.  * Side effects:
  998.  *    Bit stream is irreversibly parsed.
  999.  *
  1000.  *--------------------------------------------------------------
  1001.  */
  1002. static void
  1003. decodeDCTCoeff(type, run, level)
  1004. int type;
  1005. unsigned int *run;
  1006. int *level;
  1007. {                                                                          
  1008.   unsigned int temp_run, index, temp_level, num_bits;                   
  1009.   unsigned short int *dct_coeff_tbl;                                       
  1010.   unsigned int value;                                     
  1011.                                                                            
  1012.   show_bits8(&index);                                                    
  1013.   if (type == DCT_COEFF_FIRST)                                             
  1014.     dct_coeff_tbl = dct_coeff_first;                                       
  1015.   else                                                                     
  1016.     dct_coeff_tbl = dct_coeff_next;                                        
  1017.                                                                            
  1018.   if (index > 3) {                                                         
  1019.     value = dct_coeff_tbl[index];                                     
  1020.     temp_run = (value & RUN_MASK) >> RUN_SHIFT;             
  1021.     if (temp_run == END_OF_BLOCK) {                                        
  1022.       *run = END_OF_BLOCK;                                                 
  1023.       *level = END_OF_BLOCK;                                               
  1024.     }                                                                      
  1025.     else {                                                                 
  1026.       num_bits = (value & NUM_MASK) + 1;                      
  1027.       flush_bits(num_bits);                                                
  1028.       if (temp_run != ESCAPE) {                                            
  1029.      value = dct_coeff_tbl[index];                                     
  1030.          temp_level = (value & LEVEL_MASK) >> LEVEL_SHIFT;                 
  1031.          *run = temp_run;                                                  
  1032.          get_bits1(&value);                                                    
  1033.          *level = value ? -temp_level : temp_level;                           
  1034.        }                                                                   
  1035.        else {    /* temp_run == ESCAPE */                                  
  1036.          get_bits6(run);                                                   
  1037.          get_bits8(&temp_level);                                           
  1038.          if (temp_level != 0 && temp_level != 128)                         
  1039.         *level = ((int) (temp_level << 24)) >> 24;                     
  1040.          else if (temp_level == 0) {                                       
  1041.             get_bits8(&temp_level);                                        
  1042.                *level = temp_level;                                           
  1043.          assert(*level >= 128);                                         
  1044.          }                                                                 
  1045.          else {                                                            
  1046.             get_bits8(&temp_level);                                        
  1047.               *level = temp_level - 256;                                     
  1048.         assert(*level <= -128 && *level >= -255);                      
  1049.          }                                                                 
  1050.        }                                                                   
  1051.     }                                                                      
  1052.   }                                                                        
  1053.   else {  
  1054.     if (index == 2) {                                                   
  1055.       show_bits10(&index);                                                 
  1056.       value = dct_coeff_tbl_2[index & 3];                                     
  1057.     }                                                                        
  1058.     else if (index == 3) {                                                   
  1059.       show_bits10(&index);                                                 
  1060.       value = dct_coeff_tbl_3[index & 3];                                     
  1061.     }                                                                        
  1062.     else if (index == 1) {                                                   
  1063.       show_bits12(&index);                                                 
  1064.       value = dct_coeff_tbl_1[index & 15];                                     
  1065.     }                                                                        
  1066.     else {   /* index == 0 */                                                
  1067.       show_bits16(&index);                                                 
  1068.       value = dct_coeff_tbl_0[index & 255];                                     
  1069.     }                                                                        
  1070.     *run = (value & RUN_MASK) >> RUN_SHIFT;               
  1071.     temp_level = (value & LEVEL_MASK) >> LEVEL_SHIFT;     
  1072.     num_bits = (value & NUM_MASK) + 1;                    
  1073.     flush_bits(num_bits);                                                  
  1074.     get_bits1(&value);                                                         
  1075.     *level = value ? -temp_level : temp_level;                                 
  1076.   }  
  1077. }
  1078.  
  1079.  
  1080. /*
  1081.  *--------------------------------------------------------------
  1082.  *
  1083.  * decodeDCTCoeffFirst --
  1084.  *
  1085.  *    Huffman Decoder for dct_coeff_first. Locations for the
  1086.  *      decoded results: run and level, are being passed as
  1087.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  1088.  *      with type DCT_COEFF_FIRST.
  1089.  *
  1090.  * Results:
  1091.  *    The decoded values of run and level for dct_coeff_first or
  1092.  *      ERROR for unbound values are placed in the locations given.
  1093.  *
  1094.  * Side effects:
  1095.  *    Bit stream is irreversibly parsed.
  1096.  *
  1097.  *--------------------------------------------------------------
  1098.  */        
  1099. void
  1100. decodeDCTCoeffFirst(run, level)
  1101. unsigned int *run;
  1102. int *level;
  1103. {
  1104.   decodeDCTCoeff(DCT_COEFF_FIRST, run, level);
  1105. }
  1106.  
  1107.  
  1108.  
  1109.  
  1110. /*
  1111.  *--------------------------------------------------------------
  1112.  *
  1113.  * decodeDCTCoeffNext --
  1114.  *
  1115.  *    Huffman Decoder for dct_coeff_first. Locations for the
  1116.  *      decoded results: run and level, are being passed as
  1117.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  1118.  *      with type DCT_COEFF_NEXT.
  1119.  *
  1120.  * Results:
  1121.  *    The decoded values of run and level for dct_coeff_next or
  1122.  *      ERROR for unbound values are placed in the locations given.
  1123.  *
  1124.  * Side effects:
  1125.  *    Bit stream is irreversibly parsed.
  1126.  *
  1127.  *--------------------------------------------------------------
  1128.  */ 
  1129. void       
  1130. decodeDCTCoeffNext(run, level)
  1131. unsigned int *run;
  1132. int *level;
  1133. {
  1134.   decodeDCTCoeff(DCT_COEFF_NEXT, run, level);
  1135. }
  1136.  
  1137.  
  1138.  
  1139.  
  1140.